package org.edu.modules.classChoise.service.impl;

import org.edu.modules.classChoise.entity.VeJwXkxz;
import org.edu.modules.classChoise.entity.VeJwXkxzxs;
import org.edu.modules.classChoise.mapper.VeJwXkxzMapper;
import org.edu.modules.classChoise.mapper.VeJwXkxzxsMapper;
import org.edu.modules.classChoise.service.IVeJwXkxzService;
import org.edu.modules.classChoise.vo.VeJwXkxzVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 选课限制
 * @Author:
 * @Date:   2021-05-14
 * @Version: V1.0
 */
@Service
public class VeJwXkxzServiceImpl extends ServiceImpl<VeJwXkxzMapper, VeJwXkxz> implements IVeJwXkxzService {
    @Autowired
    private VeJwXkxzMapper veJwXkxzMapper;
    @Autowired
    private VeJwXkxzxsMapper veJwXkxzxsMapper;

    @Override
    public int changeDelStatusByIds(String[] ids, int isDelete) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ids", ids);
        map.put("isDelete", isDelete);
        return veJwXkxzMapper.changeDelStatusByIds(map);
    }

    @Override
    public int changeStatusByIds(String[] ids, int isDelete) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ids", ids);
        map.put("status", isDelete);
        return veJwXkxzMapper.changeStatusByIds(map);
    }

    @Override
    @Transactional
    public boolean save(VeJwXkxzVO veJwXkxzVO) {
        VeJwXkxz veJwXkxz = new VeJwXkxz();
        BeanUtils.copyProperties(veJwXkxzVO, veJwXkxz);
        try {
            int ret = veJwXkxzMapper.insert(veJwXkxz);
            if(ret<1){
                return false;
            }
            int xzId = veJwXkxz.getId();
            for(VeJwXkxzxs veJwXkxzxs : veJwXkxzVO.getStudents()){
                veJwXkxzxs.setXzid(xzId);
                veJwXkxzxsMapper.insert(veJwXkxzxs);
            }
            return true;
        }
        catch (Exception ex){
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateById(VeJwXkxzVO veJwXkxzVO) {
        VeJwXkxz veJwXkxz = new VeJwXkxz();
        BeanUtils.copyProperties(veJwXkxzVO, veJwXkxz);
        try {
            int ret = veJwXkxzMapper.updateById(veJwXkxz);
            if(ret<1){
                return false;
            }
            int xzId = veJwXkxz.getId();
            veJwXkxzxsMapper.deleteByXzid(xzId);
            for(VeJwXkxzxs veJwXkxzxs : veJwXkxzVO.getStudents()){
                veJwXkxzxs.setXzid(xzId);
                veJwXkxzxsMapper.insert(veJwXkxzxs);
            }
            return true;
        }
        catch (Exception ex){
            return false;
        }
    }

}
